package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "套餐管理")
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    /**
     * 套餐条件分页查询展示
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> queryByPage(SetmealPageQueryDTO dto){
        log.info("分页查询信息：{}",dto);
        PageResult p = setmealService.queryByPage(dto);
        return Result.success(p);
    }

    /**
     * 新增套餐
     * @param dto
     * @return
     */
    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmeal",key = "#dto.categoryId")//key: setmealCache::100
    public Result save(@RequestBody SetmealDTO dto){
        log.info("新增的套餐内容是：{}",dto);
        setmealService.save(dto);
        return Result.success();
    }

    /**
     * 套餐状态修改
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("套餐状态修改")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result updateSattus(@PathVariable Integer status,Long id){
        log.info("修改套餐id=={}，状态改为=={}",id,status);
        setmealService.updateSattus(status,id);
        return Result.success();
    }

    /**
     * 根据套餐id查询回显套餐
     * @param id
     * @return
     */
    @ApiOperation("根据套餐id查询回显套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> queryById(@PathVariable Long id){
        log.info("根据菜品id=={}查询回显套餐",id);
        SetmealVO setmealVO= setmealService.queryById(id);
        return Result.success(setmealVO);
    }


    /**
     * 修改套餐内容
     * @param dto
     * @return
     */
    @ApiOperation("修改套餐内容")
    @PutMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result update(@RequestBody SetmealDTO dto){
        log.info("修改套餐信息：{}",dto);
        setmealService.update(dto);
        return Result.success();
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @ApiOperation("删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("要删除的套餐集合是：{}",ids);
        setmealService.delete(ids);
        return Result.success();
    }

}
